package com.cf.r1046;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;

public class c {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        int T = Integer.parseInt(br.readLine());
        while (T-- > 0) {
            solve();
        }
        out.flush();
        out.close();
        br.close();
    }

    static void solve() throws IOException {
        String[] in = br.readLine().split(" ");
        n = Integer.parseInt(in[0]);
        in = br.readLine().split(" ");
        a = new int[n];
        int max = 0;
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(in[i]);
            if (a[i] > max) max = a[i];
        }
        // 满足当前数字为整洁数组的最后下标位置数组，为-1表示后续没有可能成为整洁数组
        place = new int[n];
        for (int i = 0; i < n; i++) place[i] = -1;

        int limit = Math.min(n, max);
        ArrayList<Integer>[] pos = new ArrayList[limit + 1];
        for (int v = 0; v <= limit; v++) pos[v] = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int v = a[i];
            if (v <= limit) pos[v].add(i);
        }

        for (int v = 1; v <= limit; v++) {
            ArrayList<Integer> list = pos[v];
            int sz = list.size();
            for (int t = 0; t < sz; t++) {
                int needIndex = t + v - 1;
                if (needIndex < sz) {
                    int startPos = list.get(t);
                    int endPos = list.get(needIndex);
                    place[startPos] = endPos;
                } else {
                    break;
                }
            }
        }

        dp = new int[n + 1];
        for (int i = 0; i <= n; i++) dp[i] = -2;
        int ans = dfs(0);
        out.println(ans);

    }
    static int[] place;
    static int n;
    static int[] a;
    static int[] dp;

    static int dfs(int i) {
        if (i >= n) return 0;
        if (dp[i] != -2) return dp[i];

        int res = dfs(i + 1);

        int x = place[i];
        if (x != -1) res = Math.max(res, a[i] + dfs(x + 1));
        dp[i] = res;
        return res;

    }

    // 计算满足整洁数组的最后下标
    static int compute(int[] a, int i) {
        int x = a[i];
        for (int j = i; j < a.length; j++) {
            if (a[j] == a[i]) {
                x--;
            }
            if (x == 0) return j;
        }
        return -1;
    }
}
